package logic

import (
	"context"
	"supermarket/userrpc/userrpcclient"

	"supermarket/api/internal/svc"
	"supermarket/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginHandlerRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	login, err := l.svcCtx.UserRpc.UserLogin(l.ctx, &userrpcclient.UserLoginRequest{
		Username: req.Username,
		Password: req.Password,
		SendSms:  req.SendSms,
		Captcha:  req.CaptCha,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "登录成功",
		Data:    login,
	}, nil
}
